import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.*;

public class Client {

	public void start() throws  Exception{
		Socket socket = new Socket("192.168.1.222",8000);
		//OutputStream out =socket.getOutputStream();

		new Thread(new Runnable(){
			public void run(){
				try{ 
				writeMsg(socket);
				}catch(Exception e){
					e.printStackTrace();
				}
				
			}
		}).start();

		new Thread(new Runnable(){
			public void run(){
				readMsg(socket);
			}
		}).start();

	}
		/*out.write("Hello".getBytes());
		out.flush();
		out.close();
		*/
	

	public void writeMsg(Socket socket)throws Exception{
		BufferedReader br =new BufferedReader(new InputStreamReader(System.in) );		
		PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream( ),"utf-8"));
		String msg ;
		//String flag;
		while (null != (msg=br.readLine( )) ){
			System.out.println(msg);
			//String tmp =String();
			pw.println("java:" +msg);
			pw.flush();
			//msg +=flag;
			//out.write(msg.getBytes());
			//out.flush();
			//bw.write(msg,0,msg.length());
			//bw.flush();
			
		}
	}

	public void readMsg(Socket socket){
		InputStream input = null;
		BufferedReader br =null;	
		try{
		input = socket.getInputStream();
		InputStreamReader isr =new InputStreamReader(input,"utf-8");
		br = new BufferedReader(isr);
		String msg ;
			while(null != (msg=br.readLine( ))){
				System.out.println(msg);	
				}
			}catch(Exception e){
				e.printStackTrace();
			}
	}


}
